package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.mapper.ChapterMapper;
import com.atguigu.guli.service.edu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-24
 */
@Api(tags = "章节管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/edu/chapter")
public class AdminChapterController {

    @Autowired
    private ChapterService chapterService;

    // 查询所有的章和小节
    @ApiOperation(value = "查询课程下所有的章节信息")
    @GetMapping("getChapterAll")
    public R getChapterAll(@RequestParam("courseId") String courseId) {
        // 查询所有章
        List<Chapter> chapterList = chapterService.getChapterAll(courseId);

        // 查询所有的章和小节
        List<Chapter> chapters = chapterService.getChaptersAndVideoS(courseId);
        return R.ok().data("items", chapters);
    }

    // 添加章节
    @ApiOperation(value = "添加章节")
    @PostMapping("saveChapter")
    public R saveChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return R.ok().message("添加章节成功");
    }

    // 删除章, 如果章下面有小节则不能删除
    @ApiOperation(value = "删除章")
    @DeleteMapping("delChapterById")
    public R delChapterById(@RequestParam("chapterId") String chapterId) {
        chapterService.delChapterById(chapterId);
        return R.ok();
    }

    // 根据id查询章
    @ApiOperation(value = "根据id查询章")
    @GetMapping("getChapterById/{chapterId}")
    public R getChapterById(@PathVariable("chapterId") String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
        return R.ok().data("item", chapter);
    }

    // 更新章
    @PutMapping("updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

}

